﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Crystal.Libs;

namespace Crystal.Mapping
{
    [Serializable]
    public class LevelMap
    {
        //Fields
        [NonSerialized]
        public Game Game;

        private List<CellBase> GameCells;

        public int MapWidth { get; private set; }
        public int MapDepth { get; private set; }

        //cTor  
        public LevelMap(Game game, int Width, int Depth)
        {
            this.Game = game;
            this.GameCells = new List<CellBase>();
            this.MapWidth = Width;
            this.MapDepth = Depth;
            this.CreateBlankMap();
        }
        public LevelMap(Game game, int[,] Data)
        {
            this.Game = game;
            this.GameCells = new List<CellBase>();
            this.MapWidth = Data.GetLength(1);
            this.MapDepth = Data.GetLength(0);
            this.CreateMapFrom(Data);
        }

        //Methods
        private void CreateBlankMap()
        {
            for (int z = 0; z < MapDepth; z++)
            {
                for (int x = 0; x < MapWidth; x++)
                {
                    GameCells.Add(new BlankFloorCell(Game, new MazePosition(x, z)));
                }
            }

            RegisterAllCells();
        }
        private void CreateMapFrom(int[,] data)
        {
            for (int z = 0; z < MapDepth; z++)
            {
                for (int x = 0; x < MapWidth; x++)
                {
                    switch ((CellIndexConversion)data[z, x])
                    {
                        case CellIndexConversion.Floor:
                            {
                                GameCells.Add(new BlankFloorCell(Game, new MazePosition(x, z)));
                                break;
                            }
                        case CellIndexConversion.Wall:
                            {
                                GameCells.Add(new WallCell(Game, new MazePosition(x, z)));
                                break;
                            }

                            //TODO: ADD MORE HERE
                        default:
                            {
                                break;
                            }
                    }
                }
            }
            RegisterAllCells();
        }

        public CellBase GetCell(MazePosition position)
        {
            return (GameCells.Where(c => c.MazePosition == position).FirstOrDefault());
        }
        public CellBase GetCell(int X, int Z)
        {
            return (GameCells.Where(c => c.MazePosition == new MazePosition(X, Z)).FirstOrDefault());
        }

        public void RegisterAllCells()
        {
            SelectionEngine.Clear();
            SelectionEngine.RegisterRange(GameCells);
        }
        public void DrawMap()
        {
            foreach (CellBase cell in GameCells)
                cell.DrawCell();
        }
    }
}
